<?php
namespace app\admin\controller;

//积分管理
class Integral extends Common
{
	//积分记录
    public function integral_list()
    {
		$where=array('a.ui_id'=>array('gt',0));
        $mobile=Request()->param('mobile');
		//注册手机号
		$status=Request()->param('status');
		//状态
        $type=Request()->param('type');
        //类型
        $start_time=strtotime(Request()->param('start_time'));
        $end_time=strtotime(Request()->param('end_time'));
        $page=Request()->param('page');
        $size=Request()->param('size');
        if($page<1){
            $page=1;
        }
        if($size<15){
            $size=15;
        }
        $now=($page-1)*$size;
		if($mobile!=''){
			$where['b.u_mobile']=array('like','%'.$mobile.'%');
		}
		if($status>0){
			if($status==1){
				$where['a.ui_status']=1;
			}else{
				$where['a.ui_status']=2;
			}
			
		}
        if($type>0){
            $where['a.ui_type']=$type;
        }
        $where_time=array(array('gt',0),array('gt',0));
        if($start_time>0){
            $where_time[]=array('egt',$start_time);
        }else{
            $start_time='';
        }
        if($end_time>0){
            $where_time[]=array('elt',$end_time);
        }else{
            $end_time='';
        }
        $where['a.ui_create_time']=$where_time;
		$user_integral=db('user_integral a')
			->field("a.*,b.u_mobile,c.c_name")
			->join('fr_user b','a.u_id = b.u_id','left')
            ->join('fr_coupon c','a.c_id = c.c_id','left')
			->order('a.ui_id desc')
			->where($where)
            ->limit("$now,$size")
			->select();
		//记录
        $count=db('user_integral a')
            ->join('fr_user b','a.u_id = b.u_id','left')
            ->join('fr_coupon c','a.c_id = c.c_id','left')
            ->where($where)
            ->count();
        //等级信息
        $page_str=page($page,$size,$count,count($user_integral));//分页
        $this->view->assign('page_str',$page_str);
		$this->view->assign('data',$user_integral);
		$this->assign('status',$status);
        $this->assign('type',$type);
		$this->assign('mobile',$mobile);
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);
        $this->assign('page',$page);
        $this->assign('size',$size);
        $this->assign('count',$count);
        return $this->view->fetch();
		//加载index页面
    }
	//单用户积分操作
	public function integral_operation(){
		if(request()->isGet()){
			//get，加载页面
			return $this->view->fetch();
		}else if(request()->isAjax()){
			$data=request()->post();			
			$mobile=trim($data['mobile']);
			//注册手机号
			$status=$data['ui_status'];
			//状态：1：增加；2：减少
            $integral=$data['ui_integral'];
            //积分
            if($integral<1){
                return return_json(array('code'=>0,'msg'=>'请输入操作积分'),'单用户积分操作');
            }
            $user=db('user a')
                ->where(array('u_mobile'=>$mobile,'u_status'=>1))
                ->find();
            if(empty($user)){
                return return_json(array('code'=>0,'msg'=>'没有该用户或冻结中'),'单用户积分操作');
            }
            db()->startTrans();
            $old_integral=$user['u_integral'];
            if($status==1){
                $new_integral= $old_integral+$integral;
            }
            if($status==2){
                $new_integral= $old_integral-$integral;
                if($new_integral<=0){
                    $new_integral=0;
                }
            }
            $res1=db('user a')
                ->where(array('u_mobile'=>$mobile,'u_status'=>1))
                ->update(array('u_integral'=>$new_integral));
            $data1=array('u_id'=>$user['u_id'],'old_integral'=>$old_integral,'new_integral'=>$new_integral,'ui_integral'=>$integral,'ui_status'=>$status,'ui_type'=>4,'ui_create_time'=>time(),'ui_add_user_id'=>session('uid'),'ui_add_user_name'=>session('user.real_name'));
            $res2=db('user_integral')->insertGetId($data1);
            if($res1!=false && $res2>0){
                db()->commit();
                return return_json(array('code'=>1,'msg'=>'操作成功'),'单用户积分操作');
            }else{
                db()->rollback();
                return return_json(array('code'=>0,'msg'=>'操作失败'),'单用户积分操作');
            }
		}
    }
    //多用户积分操作
    public function integral_operations(){
        if(request()->isGet()){
            //get，加载页面
            return $this->view->fetch();
        }else if(request()->isAjax()){
            $data=request()->post();
            $mobiles=explode("|",trim($data['mobiles']));
            //注册手机号
            $status=$data['ui_status'];
            //状态：1：增加；2：减少
            $integral=$data['ui_integral'];
            //积分
            if(empty($mobiles)){
                return return_json(array('code'=>0,'msg'=>'请输入用户注册手机号'),'多用户积分操作');
            }
            if($integral<1){
                return return_json(array('code'=>0,'msg'=>'请输入操作积分'),'多用户积分操作');
            }
            db()->startTrans();
            $res1=false;
            $data1=array();
            foreach($mobiles as $k=>$v){
                $user=db('user a')
                    ->where(array('u_mobile'=>$v,'u_status'=>1))
                    ->find();
                if(!empty($user)){
                    $old_integral=$user['u_integral'];
                    if($status==1){
                        $new_integral= $old_integral+$integral;
                    }
                    if($status==2){
                        $new_integral= $old_integral-$integral;
                        if($new_integral<=0){
                            $new_integral=0;
                        }
                    }
                    $res1=db('user a')
                        ->where(array('u_mobile'=>$v,'u_status'=>1))
                        ->update(array('u_integral'=>$new_integral));
                    $data1[]=array('u_id'=>$user['u_id'],'old_integral'=>$old_integral,'new_integral'=>$new_integral,'ui_integral'=>$integral,'ui_status'=>$status,'ui_type'=>4,'ui_create_time'=>time(),'ui_add_user_id'=>session('uid'),'ui_add_user_name'=>session('user.real_name'));
                }
            }


            $res2=db('user_integral')->insertAll($data1);
            if($res1!=false && $res2!=false){
                db()->commit();
                return return_json(array('code'=>1,'msg'=>'操作成功'),'多用户积分操作');
            }else{
                db()->rollback();
                return return_json(array('code'=>0,'msg'=>'操作失败'),'多用户积分操作');
            }
        }
    }
    //用户积分操作
    public function user_integral_operation(){
        if(request()->isGet()){
            //get，加载页面
            $id=request()->get('id');
            $user=db('user')
                ->where(array('u_id'=>$id))
                ->find();
            $this->assign('user',$user);
            return $this->view->fetch();
        }else if(request()->isAjax()){
            $data=request()->post();
            $mobile=trim($data['mobile']);
            //注册手机号
            $status=$data['ui_status'];
            //状态：1：增加；2：减少
            $integral=$data['ui_integral'];
            //积分
            if($integral<1){
                return return_json(array('code'=>0,'msg'=>'请输入操作积分'),'用户积分操作');
            }
            $user=db('user a')
                ->where(array('u_mobile'=>$mobile,'u_status'=>1))
                ->find();
            if(empty($user)){
                return return_json(array('code'=>0,'msg'=>'没有该用户或冻结中'),'用户积分操作');
            }
            db()->startTrans();
            $old_integral=$user['u_integral'];
            if($status==1){
                $new_integral= $old_integral+$integral;
            }
            if($status==2){
                $new_integral= $old_integral-$integral;
                if($new_integral<=0){
                    $new_integral=0;
                }
            }
            $res1=db('user a')
                ->where(array('u_mobile'=>$mobile,'u_status'=>1))
                ->update(array('u_integral'=>$new_integral));
            $data1=array('u_id'=>$user['u_id'],'old_integral'=>$old_integral,'new_integral'=>$new_integral,'ui_integral'=>$integral,'ui_status'=>$status,'ui_type'=>4,'ui_create_time'=>time(),'ui_add_user_id'=>session('uid'),'ui_add_user_name'=>session('user.real_name'));
            $res2=db('user_integral')->insertGetId($data1);
            if($res1!=false && $res2>0){
                db()->commit();
                return return_json(array('code'=>1,'msg'=>'操作成功'),'用户积分操作');
            }else{
                db()->rollback();
                return return_json(array('code'=>0,'msg'=>'操作失败'),'用户积分操作');
            }
        }
    }
}
